#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/9/23 19:41
# @Author  : Crissu
# @Site    : 
# @File    : rotate.py
# @Software: PyCharm
import cv2, multiprocessing, os

from utils import universal_util
from configs import rotate_config


def image_rotation(mode=1):
    # nameList = universal_util.GetImageSrc(rotate_config.FromPath)
    # print(nameList)
    classList = os.listdir(rotate_config.FromPath)
    print(classList)

    # 选择模式
    # 0: 需要补边，变成正方形，再resize
    # 1: 需要裁剪长的边跟短边一样长，然后resize
    if mode == 0:
        dealFunc = rotate_X3_0
    if mode == 1:
        dealFunc = rotate_X3_1
    # 创建路径
    universal_util.MakeFloder(rotate_config.ToPath)
    pool = multiprocessing.Pool(16)
    pool.map(dealFunc, classList)


def rotate_X3_1(className):
    path = rotate_config.FromPath + className + "/"
    nameList = os.listdir(path)
    for name in nameList:
        # 读取图片
        rawPath = path + name
        img = cv2.imread(rawPath)
        # 顺时针旋转90
        trans_img_1 = cv2.transpose(img)
        new_img_1 = cv2.flip(trans_img_1, 1)
        # 获取随机名
        # random_name = universal_util.GetRandomName()
        # saveName = "X1_" + random_name + "_" + name
        saveName = "X1_" + name
        savePath = rotate_config.ToPath + className + "/" + saveName
        cv2.imwrite(savePath, new_img_1)
        print(name, " 旋转90完毕")

        # 顺时针旋转90
        trans_img_2 = cv2.transpose(new_img_1)
        new_img_2 = cv2.flip(trans_img_2, 1)
        # 获取随机名
        # random_name = universal_util.GetRandomName()
        saveName = "X2_" + name
        savePath = rotate_config.ToPath + className + "/" + saveName
        cv2.imwrite(savePath, new_img_2)
        print(name, " 旋转180完毕")

        # 顺时针旋转90
        trans_img_3 = cv2.transpose(new_img_2)
        new_img_3 = cv2.flip(trans_img_3, 1)
        # 获取随机名
        # random_name = universal_util.GetRandomName()
        # saveName = "X2_" + random_name + "_" + name
        saveName = "X3_" + name
        savePath = rotate_config.ToPath + className + "/" + saveName
        cv2.imwrite(savePath, new_img_3)
        print(name, " 旋转270完毕")

        print("rotate_X3_1 - ", rotate_config.ToPath + className + "/" + name, " - done")



def rotate_X3_0(name):
    # 读取图片
    imgPath = rotate_config.FromPath + name
    img = cv2.imread(imgPath)
    # 顺时针旋转90
    trans_img_1 = cv2.transpose(img)
    new_img_1 = cv2.flip(trans_img_1, 1)
    # 获取随机名
    random_name = universal_util.GetRandomName()
    saveName = "X1_" + random_name + "_" + name
    savePath = rotate_config.ToPath + saveName
    cv2.imwrite(savePath, new_img_1)
    print(name, " 旋转90完毕")

    # 顺时针旋转90
    trans_img_2 = cv2.transpose(new_img_1)
    new_img_2 = cv2.flip(trans_img_2, 1)
    # 获取随机名
    random_name = universal_util.GetRandomName()
    saveName = "X2_" + random_name + "_" + name
    savePath = rotate_config.ToPath + saveName
    cv2.imwrite(savePath, new_img_2)
    print(name, " 旋转180完毕")

    # 顺时针旋转90
    trans_img_3 = cv2.transpose(new_img_2)
    new_img_3 = cv2.flip(trans_img_3, 1)
    # 获取随机名
    random_name = universal_util.GetRandomName()
    saveName = "X2_" + random_name + "_" + name
    savePath = rotate_config.ToPath + saveName
    cv2.imwrite(savePath, new_img_3)
    print(name, " 旋转270完毕")














